*****Purpose   : Used in "JFIES.do" for creating unique household ID from the 
*****            official id for them. Since the official ID for each household may 
*****            be changed during survey period, we have to make consistent ID, which
*****            can uniquely identify a household. See Unayama (2011) for details 
*****            (Available only in Japanese)
*****<Reference>
*****  Unayama, Takashi (2011) "Household ID in Japanese Family Income and Expenditure
*****  Survey: Its Construction", Kobe University Kokumin-Keizaizasshi (in Japanese).


replace serial_number=99 if year==1981&city_code==11204&unit_code==71&household_code==54
replace serial_number=99 if year==1981&city_code==26100&unit_code==51&household_code==12&serial_number==1
replace serial_number=99 if year==1986&city_code==27100&unit_code==122&household_code==5&serial_number==53
replace serial_number=99 if year==1988&city_code==34204&unit_code==21&household_code==3&serial_number==45

replace city_code=40221 if city_code==40302
replace city_code=34213 if city_code==34322
replace city_code=4100 if city_code==4201
replace city_code=12100 if city_code==12201
replace city_code=8223 if city_code==8423
replace city_code=11244 if city_code==11204
replace city_code=12232 if city_code==12326
replace city_code=11100 if city_code==11244
replace city_code=32209 if city_code==32381
replace city_code=10449 if city_code==10446
replace city_code=11202 if city_code==11403
replace city_code=16211 if city_code==16203
replace city_code=17463 if city_code==17423
replace city_code=22100 if city_code==22201
replace city_code=22226 if city_code==22422
replace city_code=23233 if city_code==23341
replace city_code=24203 if city_code==24462
replace city_code=25214 if city_code==25464
replace city_code=28229 if city_code==28211
replace city_code=28446 if city_code==28441
replace city_code=30208 if city_code==30322
replace city_code=33211 if city_code==33344
replace city_code=34202 if city_code==34311
replace city_code=35216 if city_code==35209
replace city_code=42204 if city_code==42306
replace city_code=43202 if city_code==43202
replace city_code=46216 if city_code==46367
replace city_code=47214 if city_code==47206
replace city_code=4215 if city_code==4502
replace city_code=8235 if city_code==8482
replace city_code=20203 if city_code==20341
replace city_code=44214 if city_code==44325
replace city_code=15100 if city_code==15201
replace city_code=22130 if city_code==22202
replace city_code=26214 if city_code==26361
replace city_code=9203 if city_code==9366
replace city_code=26214 if city_code==26361

replace household_code=200+household_code if period<m(1999m7)
replace household_code=floor(household_code/10)
replace household_code=household_code-5 if mod(household_code,10)>2
replace household_code=household_code-50 if household_code>30

replace serial_number=99 if year==1981&city_code==11204&unit_code==71&household_code==54
replace serial_number=99 if year==1981&month==8&city_code==26100&unit_code==51&household_code==12&serial_number==1
replace serial_number=99 if year==1986&city_code==27100&unit_code==122&household_code==5&serial_number==53
replace serial_number=99 if year==1988&city_code==34204&unit_code==21&household_code==3&serial_number==45


gen start_month=floor(unit_code/10)


gen interview=month-start_month+1 if month>=start_month
replace interview=(month+12)-start_month+1 if month<start_month


replace start_month=mod((start_month+(interview>6)*6),12)
replace start_month=12 if start_month==0


gen start_year=year-(start_month>month)


replace interview=interview-6 if interview>6


sort start_year start_month city_code unit_code household_code serial_number year month
by start_year start_month  city_code unit_code household_code serial_number: gen n_interview=_n


sort start_year start_month n_interview city_code interview
by start_year start_month n_interview: gen id_temp=_n if n_interview==1
recast long id_temp
replace id_temp=0 if n_interview>1

egen id=max(id_temp), by(start_year start_month city_code unit_code household_code serial_number)

recast long id
replace id=start_year*1000000+start_month*10000+id
format id %10.0f

gen pref=floor(city_code/1000)
gen city_size=floor(city_code/100)-pref*10

drop start_month start_year id_temp household_code serial_number







